package com.ysq.nfd.sbatis;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

/**
 * 自己写的类似 mybatis 的功能
 * 
 * @author admin
 *
 */
public class SimpleBatis {

	private Connection con;

	private SimpleBatis(Connection con) {
		this.con = con;
	}

	public Object findForObject(String sql, Object param, Class<?> returnClass) throws Exception {

		SqlSession session = null;
		Object object = null;
		try {

			object = session.selectOne(sql, param);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw e;
		}
		return object;
	}

	public Object findForList(String sql, Object param, Class<?> returnClass) throws Exception {
		SqlSession session = null;
		Object object = null;
		try {

			object = session.selectList(sql, param);
		} catch (Exception e) {
			throw e;
		}
		return object;
	}

	private Object save(String sql, Object paramObj, Class<?> returnClass, boolean returnKey) {

		SqlSession session = null;
		Object object = null;
		try {
			object = session.insert(sql, paramObj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}

	/**
	 * 插入数据
	 * 
	 * @param sql
	 * @param paramObj
	 * @param returnClass
	 * @return
	 * @throws Exception
	 */
	public Object save(String sql, Object paramObj, Class<?> returnClass) throws Exception {
		SqlSession session = null;
		Object object = null;
		try {
			object = session.insert(sql, paramObj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}

	/**
	 * 保存时返回主键
	 * 
	 * @param sql
	 * @param paramObj
	 * @param returnClass
	 * @return
	 * @throws Exception
	 */
	public Object saveReturnKey(String sql, Object paramObj, Class<?> returnClass) throws Exception {
		SqlSession session = null;
		Object object = null;
		try {

			object = session.insert(sql, paramObj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}

	public int update(String sql, Object param, Class<?> returnClass) throws Exception {

		SqlSession session = null;
		Object object = null;
		try {

			object = session.update(sql, param);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public int delete(String sql, Object param) throws Exception {
		SqlSession session = null;
		Object object = null;
		try {

			object = session.delete(sql, param);
		} catch (Exception e) {
			throw e;
		}
		return 0;
	}

	public PreparedStatement initStatement(String sql, Object param) {
		if (param.getClass().isPrimitive()) {

		}

		return null;
	}

	/*********************************************************************/

	public Object deleteBatch(String sql, List<Object> list) throws Exception {
		System.out.println("does not support yet");

		return null;
	}

	public Object saveBatch(String sql, List<Object> list) throws Exception {
		System.out.println("does not support yet");

		return null;
	}

	public Object updateBatch(String sql, List<Object> list) throws Exception {
		System.out.println("does not support yet");

		return null;
	}

	/*****************************************************************/

	public static void main(String[] args) throws Exception{

		int a = 1;

		Object param = a;

		if (param.getClass().isPrimitive()) {
			System.out.println("================= 基本类型 ===============");

		}
		int i = 0;
		
		ClassLoader cl = param.getClass().getClassLoader();
		
		PreparedStatement pst = null;
		if (param instanceof Integer) {
			int value = ((Integer) param).intValue();
			
			pst.setInt(i + 1, value);
			
		} else if (param instanceof String) {
			String s = (String) param;
			pst.setString(i + 1, s);
		} else if (param instanceof Double) {
			double d = ((Double) param).doubleValue();
			pst.setDouble(i + 1, d);
		} else if (param instanceof Float) {
			float f = ((Float) param).floatValue();
			pst.setFloat(i + 1, f);
		} else if (param instanceof Long) {
			long l = ((Long) param).longValue();
			pst.setLong(i + 1, l);
		} else if (param instanceof Boolean) {
			boolean b = ((Boolean) param).booleanValue();
			pst.setBoolean(i + 1, b);
		}

	}

}
